This is Hacker Public Radio episode 3,911 from Monday 31 July 2023.
Today's show is entitled, an overview of the Act Command.
It is part of the series Lightweight Apps.
It is the 150th show of Dave Morris, and is about 21 minutes long.
It carries an explicit flag.
The summary is a Pearl-based Grap-like tool that can search by file time.
Hello everyone, welcome to Hacker Public Radio.
My name is Dave Morris, and I'm talking today about a command, which I have put into the
category or the series of Lightweight Apps.
So what this is, is a Pearl-based tool that behaves like Grap.
It actually uses the name Beyond Grap on the website, which I refer to in the notes.
So this tool is called ACK, quite sure why, but it's a good thing.
And it's got three main features that I use.
I don't use it a huge lot.
It's not a thing I use for every search of a file, but it's great for certain things.
First of all, it can restrict the searches to files of a particular type.
So there's a way of classifying files in terms of type, which I'll talk about in a minute,
and it will search only those.
The regular expressions that it uses, I mean, Grap will handle, plain text, stuff,
and regular expressions of various sorts, including a Pearl-1.
But this one is only Pearl that it uses.
I think it's actually possible to simplify it, but the default is Pearl anyway.
And Pearl, of course, has one of the most powerful feature-rich types of regular expressions.
So that's to me, that's fantastic.
And it's got features like you can limit the search area within a file if you want to.
I'm not going to go into that.
There's a lot to say here if I was to dig deeply into all aspects of this command,
and I'm not going to do it because I don't want to.
Well, you to death, it's almost a series in itself if I were to go into that level of detail.
It's fantastic, I believe, but it's a little complex to use,
and I use it mainly in special cases where I need the features I've just mentioned.
So I'll just give you a flavor of what it can do and leave you to research it more if it sounds interesting.
So you can install it in the usual sorts of ways.
I actually installed it as a package with it.
I used Debian, so I used App to do it, App to install.
So do App to install, App can, that's fine.
I only, as I was preparing the show, I noticed that I was using version 3.6.0.
And that's a little bit behind, and there's a new version 3.7.0,
which you can get details about from the website.
It suggests that you might want to install it as a Pearl module using C-Pan,
but if you're not a Pearl user, these are things you might not want to take on board.
Let's just talk briefly about Pearl regular expressions,
as I said, they're very sophisticated and have grown tremendously over years.
I think Pearl was grand breaker in terms of regular expressions,
because a lot of other regular expression engines follow Pearl's lead.
It's certainly the power of our use, Pearl of quite a long time now,
and the power of it is quite spectacular.
What happened during the last number of years was that there came to be a thing called
the Pearl compatible regular expressions library.
This was put together by a guy called Philip Hazelor from Cambridge University.
He did this in 1997. I was particularly interested in this,
because I was working at RAN XM, the male transfer agent,
and PCR-E, Pearl of a regular expressions, were implemented within it,
and Philip Hazel was the author of XM.
So he did this, and PCR-E is available in quite a lot of other areas.
I think at one point Python used it, I'm not sure it does now, but anyway.
But since the original PCR-E, there's come a version called PCR-E2,
so he's still developing it, and it is very widely used.
The act documentation refers you to various Pearl manuals for detail to use
the regular expression syntax in detail, although you can use it in a simplistic way,
which is what I do mostly. I don't use the full power of it, but it is quite nice
to be able to get into some of the Pearl stuff.
But there's plenty of documentation if you're interested.
In fact, Gnu-Grap, which would be the one that most people will have on a Linux system,
it can use Pearl compatible regular expressions when doing it's matching,
but I can't remember what the option is. Mine is a cabrical piece,
not sure, but if you look at the documentation for Gnu-Grap, it says this is experimental.
I've not really gotten to using that in a big way.
So let's talk about the file type, as you mentioned before.
So this act command has got rules for recognizing file types,
and it does this by looking at the name extensions,
fairly obviously, so dot HTML or dot py.
It can also look at the file's contents to see the sort of first line,
which has got, what do they call that? Some sort of magic thingy.
That's you determine what a file is called. I completely forgot the terminology.
You can find out what Act knows about in terms of types by giving it the option
hifin-hifin help hifin types, or you can use Act based hifin-hifin dump.
Some of the examples are CC, as a type, and those are C files,
Haskell, Haskell files, Lua, Lua files, Python for Python files,
Shell for Bash and other Shell command files.
And these names can be used with the options,
Lua case hifin-hifin-hifin-hifin-hifin type equals the type name,
and also by preceding the type name with two dashes, if you wish.
I think that might be the particular way of doing it might not be available in the future,
because it says it's deprecates it somewhere. That's what I use.
You can also say files not of this type by using hifin-hifin-hifin-hifin type equals no
followed by the type string. Anyway, now I use that much of a sub. I don't usually want to look
at files which are not Shell script, because I'll be everywhere the else and I don't want it.
But it's good that you have it. So we've got a little example here to check files in the current
directory, which are Shell scripts, then you might want to do this with the command
AC as base. Hifin-hifin-shel, space, and then the search string, which in my case is declare.
And I'm actually in the directory where I prepare my, it's better shows, and it finds
one in title bash snippet using co-proc with their skew light. It finds a shell script there
and shows me online 11. There is the occurrence of the word declare. And it's quite nice that
it by default gives you the line number. It also does coloring of these things when you search
all of which you can turn off and mess around with to enormous degrees. You can add your own
file types to hack and there's a configuration file called .acrcacrc, which you can add more types
to and talk about that next. So that configuration file .acrc, and it contains, as the manual says,
command line options that are pre-pended to the command line before processing. So it's useful
way to add new types or modify existing ones. There are a number of places that can be placed
and the documentation will tell you that. But I put mine in my home directory where I keep all my
other configuration files. It can be another other more regular places in the Comfort directory
You can actually create a new .acrc with the option hif and hif and create hif and a ckrc.
So it will write out an example series of settings on sanded out and you can just pipe it to
the file. That's all the defaults that have built into the script, but it means that you've
got them somewhere where you can change them if you wish. Now I have a lot of marked down files
in my directory where I do all my hbr talks. I write everything in marked down. And for some
reason I'm not sure why I did this originally gave them the extension of .mkd. I must have seen
somebody else do that or just seemed like the right thing and I wasn't sure what was better. But
act recognises .md and .markdown as signaling marked down file. So I wanted to add .mkd to the list.
And it was pretty simple to do. There's two commands you can use within your
accuracy. There's one which is dash dash type dash add equals marked down .call on .ext
call on .mkd. So it depends the particular extension to the existing list or you can you can use
dash dash type dash set equals marked down .call on .ext .call on .md .call .mkd .call
marked down. Well that does is to replace the existing settings. That's why it uses set as
supposed to add after type. And you just give it the list. It currently existing list plus
whatever else you've added to it. You can put comments in the file too. So if you then dump the
settings. So in done this you can see that marked down is listed with .md .markdown and .mkd
as detectable extensions. So if I do a search for marked down in the directory I'm in where my
various HPR shows live. I keep them all around forever .ackspace-dashmarkdown
space then in quotes in a well-in-law case space ear. I get back one match, one file match and then I
get a bunch of lines that contain the string in it ear. Now there are a lot of options to act
the general usage pattern for using the command is that you type act followed by list of options
followed by a pattern which is the thing you're matching usually in in files but but come on to that
more in a moment and followed by an optional list of files or directories. If you don't give
a list of files or directories then it will look at the current directory and will recurst
down into subdirectories and the pattern that I mentioned is the PCRE search string which is usually
enclosed in single quotes for so it doesn't get interpreted by the shell. There are some
cases where you don't use a pattern but look at that briefly in a moment. They can look at the full
documentation for the usual man act command and the alternative to doing that is to use
act itself to report it's man page which is act space-dashman. There is also an option
dash-dash help which gives a summary of all the available options which is actually find
more useful because it's usually options I'm trying to remember and it's to scan through
rather the full documentation. There's got a few options to refer to here. There's quite a lot
that are specific to act and some of the sentences meant to be a prep standing there are some
which are common to prep but I won't look at too many. Well the first one is one that you do find
in prep which is dash-i and that makes the the pattern matching case incentive. It's possible to
do that within the pearl expression. You can say I want these only to be matched with. I don't care
about the case when matching but it's a lot easier to use it as an option I find. Then we get to
dash-f which is about searching for files by name so it only prints the files that would be
searched. It comes back with the list of file names. It doesn't do any searching but it's useful
for finding things within a directory which are a particular type or match a pattern or whatever.
dash-g is the same as dash-f but in this case you use a pattern and you look for files which match
that pattern whose names match the pattern like the contents. But it that overlaps what you get
with the type business so it's useful in some cases but can be a little bit misleading. Then we get
dash-l which reports the file names which contain matches for a given pattern. So it's not actually
showing you the matches but you're showing you the file which would match and the dash capital
hell reports file names which do not match the pattern. Then you've got dash-c which you'll
get in-grap which reports file names and the numbers of matches when you use it. So it actually
reports all files that match whatever it is you're matching against in terms of file types or names.
It reports them all and it gives you a count of zero if there are no matches which I find bit
bit of pain not that useful but if you use it with hyphen L then you only see the names of
files that have matches and a count of the matches. So I've just used it yesterday looking through
a bunch of files to see if any of them had a particular string in them because I needed to
edit it because it was a grammar grammatical error and so using the hyphen C and I from L was
a great way to do it and then we have dash-w which forces the search pattern to match only
whole words. So a lot of times there isn't a way of doing that in-grap that I know of.
I think you're wrong actually. I think some of the the regular expression capabilities of
Grap will allow you to do that anyway but it's sometimes useful to be able to say look that's
sequence of characters I've just given you that I want you to look for is the word or our words
as opposed to just being an ABC anywhere in any any text. So there's a lot. There's actually a lot
of power in this possibly too much. I don't use it all by any means. I've got a few examples.
The first example is looking for all mark-down files in a directory. So first option was to use the
dash f option. So I typed ack space dash dash mark-down space dash f space and then the
name of a sub-directory nightcore tube torch. So sure I did some time ago and it comes back with the list
of names which are the mark-down files within the within that directory or mkd files.
Now there are many other ways you could do that. You could use the find command to find them.
That's that would have been what I would have used in the past but I find ack just as a nice
job. But given alternative here using the dash g option so ack space dash g and then we're using
a pattern. The pattern is open-quote backslash dot which matches an actual dot mkd dollar
closed quote or that are saying any file name you get back which ends with dot mkd and then the
name of directory nightcore tube torch and it comes like the same file names achieved by a different
method. I think I would use the former in pretty much all cases but if you don't have a type that you
can use to do the search then the g thing is an alternative. So what about finding the names of files,
listing the names of files that contain a match with some string and the number of matches per file.
So this one is the act command followed by dash dash mark done to look into markdown files again.
This time I'm using dash LCI so I've can catonated three of the options together which you can do
with single character options and by the way these options are usually I think in all cases have
no not all cases but many cases have a single character version and a double hyphen followed by a
long version so but I'm using the short versions here for demonstration purposes so LCI means
use the options dash L dash C and dash I. Now my match string my pattern is
open quote, bxb ea rxb now that's using one of the pearl regular expression capabilities which is
to denote a boundary and this case a word boundary. So the the bxb is can be an opening
boundary or closing one. So it's saying look for the word ea ea r which is a word a
standalone word. Now there are other ways of doing that you can do something like that in grep
but the regular expression syntax is very in the way that this word boundary thing is done and
gets a little bit messy but the point is that it's separating out the sequence ea r from
for example in the word pearl as an ea r but you don't want pearl to be returned because it's not
a word I mean it's a word but I mean it doesn't got the word ea ea in it so example three
just looking forward in a simpler way similar to example two where we used the bxb boundaries
you can achieve this alternatively by making the pattern simpler just the word ea r ea and
preceding it with dash w which as I mentioned before says treat this as a word or words I think but
I might have experiment with that but in both of these cases what you get back is list of
file names and each file name is followed by colon and a number which tells you how many matches
for that word exist in the file. There's the same in both cases of course in example two and three
so I thought about stop with the examples here because it's getting a bit too far a bit too long
otherwise so that's it then there's some references to the pearl reference manuals and tutorials
and stuff and the the site where ack can be found and details about it so a little bit of that
that's the end and I hope you found that useful okay then bye
and our synced of net on this otherwise stages they show is released on their creative
comments attribution for pointo international license